package com.tange.module.login.facebook;

import android.app.Activity;
import android.content.Intent;

import com.facebook.AccessToken;
import com.facebook.GraphRequest;
import com.facebook.HttpMethod;
import com.facebook.login.LoginManager;
import com.tg.appcommon.android.TGLog;

public class FacebookLoginProxy {

    private static final String TAG = "FacebookLoginProxy";

    public static void launch(Activity activity, int requestCode) {
        Intent intent = new Intent(activity, FacebookLoginActivity.class);
        activity.startActivityForResult(intent, requestCode);
    }

    public static void logout(Activity activity, Runnable runnable) {
        if (AccessToken.getCurrentAccessToken() != null) {
            new GraphRequest(
                    AccessToken.getCurrentAccessToken(), "/me/permissions/", null, HttpMethod.DELETE,
                    graphResponse -> {
                        TGLog.i(TAG, "logout graphResponse = " + graphResponse);

                        AccessToken.setCurrentAccessToken(null);
                        LoginManager.getInstance().logOut();

                        if (runnable != null) {
                            runnable.run();
                        }
                    }
            ).executeAsync();

            TGLog.i(TAG, "logout request send !");
        }
    }
}
